"""
找到带有指定标签的答案
"""
import json, sys, os, xmltodict, csv
from os.path import join
from utils import *
from tqdm import tqdm
import shutil
import re

# sys.argv[]是一个list，里面的项是用户输入的参数，0代表代码本身文件路径，1代表用户输入的第一个参数
PATH = sys.argv[1]
DIR = PATH.replace('extracted/','')

# print("importing",DIR)

file = join(PATH,'Posts.xml')

print("importing ",file)
os.system("pause")

# clean方法用于移除x中的特殊字符
def clean(x):
    #neo4j-import doesn't support: multiline (coming soon), quotes next to each other and escape quotes with '\""'
    return x.replace('\n','').replace('\r','').replace('\\','').replace('"','')

def open_csv(name):
    # open方法中newline参数赋值为"",避免csv文件中每行数据多一行空格

    return csv.writer(open('csvs/{}.csv'.format(name), 'w', newline=''), doublequote=False, escapechar='\\')

def remove_h5_tags(content):
    return re.sub('<[^<]+?>', '', content).replace('\n', '').strip()


try:
    # 该方法递归删除文件夹下的所有子文件夹和子文件
    shutil.rmtree('csvs/')
    # print("hehe")
except:
    pass
os.mkdir('csvs')

posts = open_csv('answers_with_tags_posts')

# 创建集合，存储带有python标签的问题的ID
ids = set()

title_and_tags = dict()

# post属性
posts.writerow(['postId:ID(Post)', 'question_title', 'tags', 'question_body', 'body'])

# enumerate 对于一个可迭代的（iterable）/可遍历的对象（如列表、字符串），enumerate将其组成一个索引序列，利用它可以同时获得索引和值
for i, line in tqdm(enumerate(open(file, encoding="gbk", errors="ignore"))):
    # strip用于移除字符串头尾指定的字符或字符序列（默认是空格或换行符）
    line = line.strip()
    if line.startswith("<row"):
        # parse方法将xml变得像json
        el = xmltodict.parse(line)['row']
        # replace_keys用于将每项的key改为小写并且移除'@'字符
        el = replace_keys(el)
        # print(el.get("id",""))
        tags = el.get("tags","")
        typeID  = el.get("posttypeid","")
        # print(type(typeID),typeID)
        # print(el)

        # 筛选条件：带有*****标签
        if tags.find("tensorflow") != -1:
            print(el.get("id",""),el.get("tags",""))
            # os.system("pause")
            if(typeID == "1"):
                post_id = el.get("id","")
                ids.add(post_id)
                # 获取问题的题目、标签和问题描述
                
                title_and_tags[post_id] = [el.get("title",""),el.get("tags",""),remove_h5_tags(el.get("body",""))]
        
        if typeID == "2":
            parentID = el.get("parentid","")

            if parentID in ids:
                body = clean(el.get("body",""))
                # 去除文本中的html标签
                # body = re.sub('<[^<]+?>', '', body).replace('\n', '').strip()
                body = remove_h5_tags(body)
                posts.writerow([
                    el.get("id",""),
                    title_and_tags[parentID][0],
                    title_and_tags[parentID][1],
                    title_and_tags[parentID][2],
                    body
                ])
        # elif typeID == "2":

# print(ids)